#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <climits>
using namespace std;

bool cmp(string x, string y)
{
    return (x.size() < y.size());
}

int main()
{
    int cases, num;
    cin >> cases;
    for (int c=1; c<=cases; ++c)
    {

        cin >> num;
        vector<string> in(num);
        for (int i=0; i<num; ++i)
        {
            cin >> in[i];
        }

        sort(in.begin(), in.end(), cmp);

        string s_min = in[0];
        int sz = s_min.size();
        int num_sub = (int)((float)sz/2*(sz+1));
        vector<string> sub;
        for (int len=sz; len>=1; --len)
        {
            for (int pos=0; pos<=sz-len; ++pos)
            {
                sub.push_back(s_min.substr(pos, len));
            }
        }

        bool flag = false;
        for (int i=0; i<num_sub; ++i)
        {
            string subs  = sub[i], rsubs = subs;
            reverse(rsubs.begin(), rsubs.end());
            unsigned int len_subs = subs.size();
            unsigned int pos = UINT_MAX, rpos = UINT_MAX, len_s;
            for (int j=0; j<num; ++j)
            {
                len_s = in[j].size();
                rpos = in[j].find(rsubs);
                pos = in[j].find(subs);
                if (pos > len_s && rpos > len_s) break;
            }
            if (pos < len_s || rpos < len_s)
            {
                flag = true;
                cout << len_subs << endl;
                break;
            }
        }
        if (flag == false) cout << 0 << endl;
    }
    return 0;
}

